
// 游戏牌局数据管理

import { EPokerStatus, ESuit } from "../ConfigEnum";
import UIPoker from "../View/UIPoker/UIPoker";
import GameEvent from "./GameEvent";

export class PokerGroup {
    public AddPoker(poker: Poker) {
        this._pokers.push(poker)
    }


    public get Pokers(): Poker[] { return this._pokers }
    private _pokers: Poker[] = []
}



export class Poker {
    public point: number = -1;
    public suit: ESuit = ESuit.HEITAO;
    private _status: EPokerStatus = EPokerStatus.CLOSE

    public get status(): EPokerStatus {
        return this._status
    }
    public set status(status:EPokerStatus) {
         this._status = status
    }


    private _view: UIPoker = null
    
    public get view(): UIPoker {
        return this._view
    }

    constructor(point: number, suit: number, status: EPokerStatus) {
        this.point = point
        this.suit = suit
        this._status = status
    }
    public Bind(view: UIPoker) {
        this._view = view
    }

    public UnBind() {
        this._view = undefined
    }
}



export default class GameDB {

    public static readonly CONST_RECEIVE_GROUPS: number = 4
    public static readonly CONST_PLAY_GROUPS: number = 7


    // public static API
    public static Create(): GameDB {
        let gameBD = new GameDB()
        return gameBD
    }


    // public API
    public Play() {
        this.shuffle(this.pokers,500)
        let tmp = this._closeAreapokers
        this._closeAreapokers = this._pokers
        this._pokers = tmp
        cc.systemEvent.emit(GameEvent.PLAY, this._pokers)

        for (let cards = GameDB.CONST_PLAY_GROUPS; cards >= 1; --cards) {
            for (let i = 0; i < cards; i++) {
                let cardGroupIndex = GameDB.CONST_PLAY_GROUPS - cards + i
                let cardGroup: PokerGroup = this._playPokerGrounp[cardGroupIndex]
                let poker = this._closeAreapokers[this._closeAreapokers.length - 1]
                this._closeAreapokers.length = this._closeAreapokers.length -1
                poker.status = i === 0 ? EPokerStatus.OPEN : EPokerStatus.CLOSE
                cardGroup.AddPoker(poker)
                cc.systemEvent.emit(GameEvent.INIT_GROUP_CARD, cardGroupIndex, GameDB.CONST_PLAY_GROUPS - cards, poker)
            }
        }


    }

    //private API
    //洗牌
    private shuffle(pokers:Poker[],count:number){
        for(let i = 0 ; i<count;i++){
            let sIdx = parseInt(''+Math.random()*pokers.length,10)
            let eIdx = parseInt(''+Math.random()*pokers.length,10)
            let tmpVal = pokers[sIdx]
            pokers[sIdx] = pokers[eIdx]
            pokers[eIdx] = tmpVal
        }
    }


    constructor() {
        // 初始化牌局布局
        for (let index = 0; index < GameDB.CONST_RECEIVE_GROUPS; index++) {
            let pokerGroup = new PokerGroup()
            this._receivePokerGroup.push(pokerGroup)

        }

        for (let index = 0; index < GameDB.CONST_PLAY_GROUPS; index++) {
            let pokerGroup = new PokerGroup()
            this._playPokerGrounp.push(pokerGroup)

        }

        // 初始化所有牌组
        for (let point = 1; point <= 13; point++) {
            for (let suit = 0; suit < 4; suit++) {
                let poker = new Poker(point, suit, EPokerStatus.CLOSE)
                this._pokers.push(poker)
            }
        }

        cc.systemEvent.emit(GameEvent.INIT_POKER, this._pokers)

    }
    /* 
        getter & setter 

    */
    public get pokers(): Poker[] { return this._pokers }
    public get closeAreaPokers(): Poker[] { return this._closeAreapokers }
    public get openAreaPokers(): Poker[] { return this._openAreapokers }
    public get receivePokerGroup(): PokerGroup[] { return this._receivePokerGroup }
    public get playPokerGrounp(): PokerGroup[] { return this._playPokerGrounp }


    // 原始所有扑克
    private _pokers: Poker[] = []
    // 盖着的扑克
    private _closeAreapokers: Poker[] = []
    // 打开的扑克
    private _openAreapokers: Poker[] = []
    // 收牌区域
    private _receivePokerGroup: PokerGroup[] = []
    // 玩牌区域
    private _playPokerGrounp: PokerGroup[] = []

}